/* Curl.h
Copyright (c) 2008 Michael Zahniser
Please note the license terms (MIT license) at the end of this document.

Header file for the Curl class, a C++ wrapper around libcurl.

An instance of Curl represents a single connection through which files
may be downloaded.
*/

#ifndef __MZ_CURL_H__
#define __MZ_CURL_H__

#include "_common/Noncopyable.h"

#include <curl/curl.h>

#include <string>

using std::string;




class Curl : public Noncopyable {
public:
	// Default constructor. The connection object is not actually created
	// until Init() is called or until it is used. This also means that
	// libcurl is not initialized until that point. To catch libcurl
	// errors early on, call Init() on your first Curl object.
	Curl();
	// Free this Curl. If it is the last Curl in existence, shut down
	// libcurl as well.
	~Curl();
	
	// Check if this object has been successfully initialized.
	operator bool() const;
	bool operator!() const;
	
	// Initialize the library, if it has not been initialized yet.
	// Also initialize this particular Curl object.
	void Init();
	// Uninitialize this Curl object. After calling this, calling any
	// of the other functions below will do nothing. This will also
	// shut down libcurl if this was the only Curl object.
	void Free();
	// Reset this Curl, stripping all the non-standard options.
	// By default, a Curl is constructed with a User-Agent matching IE.
	void Reset();
	
	// Add a header. Adding a new header with the same name as an old
	// one will replace the old one.
	void AddHeader(const string &name, const string &contents);
	
	// Download a file from the given URL into the given string.
	void Get(const string &url, string &buffer);
	// Download a file and return a reference to a string containing
	// the file. This reference is only valid until the next call to
	// Download() on this Curl.
	const string &Get(const string &url);
	
	
private:
	CURL *curl;
	curl_slist *headers;
	string buffer;
	
	
private:
	// Create a CURL object. If this is the first such object,
	// initialize libcurl first.
	static CURL *MakeCURL();
	// Free a CURL object. If it is the last one, also shut down
	// libcurl.
	static void FreeCURL(CURL *curl);
	
	// This function is used to write data to a string.
    static size_t WriteFunction(void *input, size_t elementSize, size_t elementCount, void *str);
	
	
private:
	static size_t instances;
};



#endif



/* Copyright (c) 2008 Michael Zahniser

Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE. */

